package com.xgo.http;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.untils.Common;

public class XinxinCrawler {
	/** 爬取的页数*/
	private int page;
	private int index = 1;
	private static String nextLink = null; 
	private static String path = "d:/xinxin/"; 
	private static String[] keywords = {"挂件", "吊坠", "批发", "手镯", "A货", "玉器", "", "", "", "", "", "", ""}; 
	private static Logger logger = LoggerFactory.getLogger(XinxinCrawler.class);
	
	
	public static void main(String[] args) {
		XinxinCrawler crawler = new XinxinCrawler();
		crawler.page = 1010;
//		crawler.run("http://xinxinyuye.com/pd.jsp?id=517&_pp=3_13");
//		crawler.run("http://xinxinyuye.com/pd.jsp?id=657&_pp=3_13#module14");
		crawler.run("http://xinxinyuye.com/pd.jsp?id=360&_pp=3_13#module14");
	}
	
	/**
	 * 递归执行商品列表信息解析
	 * @param szUrl
	 */
	public void run(String szUrl){
		if(!szUrl.contains("http://")){
			szUrl = "http://xinxinyuye.com/" + szUrl;
		}
		if(page > 0){
			//下载页面
			Document doc = null;
			try {
				System.out.println(szUrl);
				URL url = new URL(szUrl);
				doc = Jsoup.parse(url, 60000);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(doc == null ){
				logger.error(" Xgo html down load failed.");
				return;
			}
			try {
				parseShop(doc);
			} catch (IOException e1) {
				logger.error(e1.getMessage());
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			page--;
			//递归调用 解析下一页
			if(nextLink != null){
				run(nextLink);
			}
		
		}
	}
		

	/**
	 * 解析
	 * @param szHtml
	 * @return
	 * @throws IOException 
	 */
	private void parseShop(Document doc) throws IOException{
		Elements nextPageA = doc.getElementsByAttributeValue("class", "pageNext").get(0).getElementsByTag("a");
		if(nextPageA != null && nextPageA.size() > 0){
			nextLink = nextPageA.attr("href");
			//获取页面商品
			Element title = doc.getElementsByAttributeValue("class", "productDetail").get(0).getElementsByAttributeValue("class", "title").get(0);
			String fileName = Common.replace(keywords, title.text());
			Elements es = doc.getElementsByAttributeValue("class", "imgContainer");
			es = es.get(0).getElementsByTag("img");
			for (Element element : es) {
				String url = element.attr("src");
				
				if(url != null && url.length() > 3){
					int prefixEnd = url.lastIndexOf("!");
					int fileFormatBegin = url.lastIndexOf(".");
					prefixEnd = prefixEnd != -1 ? prefixEnd : fileFormatBegin;
					String fileFormat = url.substring(fileFormatBegin);
					url = url.substring(0, prefixEnd) + fileFormat;
					
					try {
						Common.download(url, path + fileName + index + fileFormat);
					} catch (IOException e) {
						throw new IOException("download image error.:" + url);
					}
					index++;
				}
			}
		}else{
			nextLink = null;
		}
	}
}

